// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: Support static xml filter by participant name
// ------------------------------------------------------------------

#ifndef INCLUDE_ERTPS_XMLPARSER_XMLENDPOINTPARSER_H_
#define INCLUDE_ERTPS_XMLPARSER_XMLENDPOINTPARSER_H_
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC

#include <cstdint>
#include <set>
#include <string>
#include <vector>

#include <tinyxml2.h>

#include <edds/rtps/builtin/data/ReaderProxyData.h>
#include <edds/rtps/builtin/data/WriterProxyData.h>
#include <ertps/xmlparser/XMLParser.h>
#include <xmlparser/XMLEndpointParserPart.h>

namespace evbs {
namespace ertps {
namespace rtps {
class ReaderProxyData;
class WriterProxyData;
}  // namespace rtps

namespace xmlparser {

using vbsutil::xmlparser::XMLP_ret;
using vbsutil::xmlparser::xmlString;

/**
 * Class StaticRTPSParticipantInfo, contains the information of writers and readers loaded from the
 *XML file.
 *@ingroup DISCOVERY_MODULE
 */
class StaticRTPSParticipantInfo {
 public:
    StaticRTPSParticipantInfo() {}

    virtual ~StaticRTPSParticipantInfo() {}

    //! RTPS PArticipant name
    std::string m_RTPSParticipantName;
    //! Vector of ReaderProxyData pointer
    std::vector<rtps::ReaderProxyData*> m_readers;
    //! Vector of ReaderProxyData pointer
    std::vector<rtps::WriterProxyData*> m_writers;
};

/**
 * Class XMLEndpointParser used to parse the XML file that contains information about remote
 * endpoints.
 * @ingroup DISCVOERYMODULE
 */
class XMLEndpointParser {
 public:
    XMLEndpointParser();
    virtual ~XMLEndpointParser();
    /**
     * Load the XML file
     * @param filename Name or data of the file to load and parse.
     * The string could contain a filename (file://) or the XML content directly (data://), filename
     * assumed if neither
     * @return True if correct.
     */
    XMLP_ret loadXMLFile(std::string& filename);

    XMLP_ret loadXMLFile(std::string& filename, std::string& participantName);
    /**
     * Load the XML node
     * @param doc Node to parse.
     * @return True if correct.
     */
    XMLP_ret loadXMLNode(tinyxml2::XMLDocument& doc);
    /**
     * Look for a reader in the previously loaded endpoints.
     * @param[in] partname RTPSParticipant name
     * @param[in] id Id of the reader
     * @param[out] rdataptr Pointer to pointer to return the information.
     * @return True if found.
     */
    XMLP_ret lookforReader(const char* partname, uint16_t id, rtps::ReaderProxyData** rdataptr);
    /**
     * Look for a writer in the previously loaded endpoints.
     * @param[in] partname RTPSParticipant name
     * @param[in] id Id of the writer
     * @param[out] wdataptr Pointer to pointer to return the information.
     * @return True if found
     */
    XMLP_ret lookforWriter(const char* partname, uint16_t id, rtps::WriterProxyData** wdataptr);

    void AssignParticipants(std::unique_ptr<vbsutil::xmlparser::XMLEndpointParserPart>&& edpxmlPtr);

 private:
    std::vector<StaticRTPSParticipantInfo*> m_RTPSParticipants;
};

}  // namespace xmlparser
}  // namespace ertps
} /* namespace evbs */

#endif  // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
#endif  // INCLUDE_ERTPS_XMLPARSER_XMLENDPOINTPARSER_H_
